package com.eolcum.evo.persistence;

import java.util.List;

//import javax.xml.bind.annotation.XmlElement;

public class Room {
	private int id;
	private int apartmentId;
	private String name;
	private double actualTemperatureSetpoint;
	private boolean actualOccupied = false;
	private double actualTemperature;
	private double actualHumidity;

	private Boolean isTemperatureControlled = false;
	private Boolean isOccupancyControlled = false;

	private List<Actuator> actuators;
	private List<Sensor> sensors;
	private List<ScheduleSetpoint> dailyTimeSchedule;

	public Room() {
		if (sensors != null) {
			for (int i = 0; i < sensors.size(); i++) {
				if (sensors.get(i).isHasOccup()) {
					setIsOccupancyControlled(true);
				}
				if (sensors.get(i).isHasTemp()) {
					isTemperatureControlled = true;
				}
			}
		}
	}

	/**
	 * @return the id
	 */
	public synchronized int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	// @XmlElement(required = true, name = "RoomId")
	public synchronized void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the apartmentId
	 */
	public synchronized int getApartmentId() {
		return apartmentId;
	}

	/**
	 * @param apartmentId
	 *            the apartmentId to set
	 */
	// @XmlElement(required = true, name = "RoomApartmentId")
	public synchronized void setApartmentId(int apartmentId) {
		this.apartmentId = apartmentId;
	}

	public synchronized List<Actuator> getActuators() {
		return actuators;
	}

	// @XmlElement(required = false, name = "RoomActuator")
	public synchronized void setActuators(List<Actuator> actuators) {
		this.actuators = actuators;
	}

	/**
	 * @return the sensors
	 */
	public synchronized List<Sensor> getSensors() {
		return sensors;
	}

	/**
	 * @param sensors
	 *            the sensors to set
	 */
	// @XmlElement(required = false, name = "RoomSensor")
	public synchronized void setSensors(List<Sensor> sensors) {
		this.sensors = sensors;
	}

	/**
	 * @return the name
	 */
	public synchronized String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	// @XmlElement(required = true, name = "RoomName")
	public synchronized void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the dailyTimeSchedule
	 */
	public synchronized List<ScheduleSetpoint> getDailyTimeSchedule() {
		return dailyTimeSchedule;
	}

	/**
	 * @param dailyTimeSchedule
	 *            the dailyTimeSchedule to set
	 */
	public synchronized void setDailyTimeSchedule(List<ScheduleSetpoint> dailyTimeSchedule) {
		this.dailyTimeSchedule = dailyTimeSchedule;
	}

	public synchronized Boolean getIsTemperatureControlled() {
		return isTemperatureControlled;
	}

	public synchronized void setIsTemperatureControlled(Boolean isTemperatureControlled) {
		this.isTemperatureControlled = isTemperatureControlled;
	}

	/**
	 * @return the activeTemperatureSetpoint
	 */
	public synchronized double getActualTemperatureSetpoint() {
		return actualTemperatureSetpoint;
	}

	/**
	 * @param activeTemperatureSetpoint
	 *            the activeTemperatureSetpoint to set
	 */
	public synchronized void setActualTemperatureSetpoint(double actualTemperatureSetpoint) {
		this.actualTemperatureSetpoint = actualTemperatureSetpoint;
	}

	public void updateActualTemperature() {
		int temperatureSensorCount = 0;
		double totalTemperature = 0;
		for (int i = 0; i < sensors.size(); i++) {
			if (sensors.get(i).isHasTemp()) {
				temperatureSensorCount++;
				totalTemperature = totalTemperature + sensors.get(i).getActiveTemp();
			}
		}
		actualTemperature =totalTemperature / temperatureSensorCount;
	}

	public Boolean getIsOccupancyControlled() {
		return isOccupancyControlled;
	}

	public void setIsOccupancyControlled(Boolean isOccupancyControlled) {
		this.isOccupancyControlled = isOccupancyControlled;
	}

	public boolean isActualOccupied() {
		return actualOccupied;
	}

	public void setActualOccupied(boolean actualOccupied) {
		this.actualOccupied = actualOccupied;
	}


	public synchronized double getActualTemperature() {
		return actualTemperature;
	}

	public synchronized void setActualTemperature(double actualTemperature) {
		this.actualTemperature = actualTemperature;
	}

	public synchronized double getActualHumidity() {
		return actualHumidity;
	}

	public synchronized void setActualHumidity(double actualHumidity) {
		this.actualHumidity = actualHumidity;
	}

	public void updateActualHumidity() {
		int humiditySensorCount = 0;
		double totalHumidity = 0;
		for (int i = 0; i < sensors.size(); i++) {
			if (sensors.get(i).isHasHumid()) {
				humiditySensorCount++;
				totalHumidity = totalHumidity + sensors.get(i).getActiveHumidity();
			}
		}
		actualHumidity = totalHumidity / humiditySensorCount;
	}

	public void updateActualOccupancy() {
		actualOccupied = false;
		for (int i = 0; i < sensors.size(); i++) {
			if (sensors.get(i).isHasOccup()) {
				actualOccupied = actualOccupied || sensors.get(i).isActiveOccupied();
			}
		}
	}

}